clear all 
capture log close

global data "/Users/ceweber/Dropbox/JPAM_TIV_code/original_data/"
global dta "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_files/"
global dataclean "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_output/"
global output "/Users/ceweber/Dropbox/JPAM_TIV_code/output/"

set more off
cd "${dataclean}"

//////////////////////////////////////////////////////////////////
///LOOK FOR INVENTORYLOTS THAT TRANSITION/////////
/////////////////////////////////////////////////////////////////

use rounding_results_weekly_expand_kendall2
sum group
local locationmax = r(max)

forvalues l =1(1)`locationmax'{
use mult_`l'.dta, clear


qui sum location 
local name = r(mean)
replace location=r(mean) if location==.

collapse (mean) location multiplier_weekly, by(weekly)
mmerge location weekly using daily_mode_collapsed_`name'.dta, ukeep(price inventoryid date usableweight inventorytype) 
drop _merge	
rename multiplier_weekly multiplier	
 drop if date==.

mmerge location weekly using rounding_results_weekly_expand_kendall2.dta, ukeep(tax_mult_none-tax_mult_both_mult) unmatched(master)
drop _merge	

***we want up to three candidate multipliers: 1, 1+sales tax, 1+all taxes (first two before tax change, third after the tax change)
replace multiplier=round(multiplier,.001)
gen mult_flag=0
//eliminate 1.005 (or less) and 1.49 (or more) and replace each of these with a multiplier of 1.
replace multiplier=1 if multiplier<=1.02
replace multiplier=1 if multiplier>=1.4899


summ multiplier
replace multiplier=1 if _n==1&r(min)>1&r(min)<.&date<20270
replace mult_flag=1 if multiplier==1&date<20270

//now pin down the sales tax multiplier
summ multiplier if multiplier>=1.065
replace multiplier=tax_mult_sales_only if r(min)>1.1&date<20270&_n>1
replace mult_flag=1 if multiplier>=1.065&multiplier<1.1&date<20270

//now pin down the both add multiplier after the tax change:
summ multiplier if date>=20270
replace multiplier =tax_mult_both_add if r(max)<1.435&date>=20270
replace mult_flag=1 if multiplier>=1.435&date>=20270
 
//get rid of all other multipliers:
replace multiplier=. if mult_flag==0
replace multiplier=round(multiplier,.001)

//get all legislated switch dates from tax_mult_both_add.  then impose these switch dates, and then look to see if another preferred switchd date
sort inventoryid date
egen groupinv=group(inventoryid)
gen tax_switch_tmp=0
replace tax_switch_tmp=1 if tax_mult_both_add[_n]!=tax_mult_both_add[_n-1]&groupinv[_n]==groupinv[_n-1]&date[_n]-date[_n-1]==1

egen tax_switch=max(tax_switch_tmp), by(date)
drop tax_switch_tmp

sort inventoryid date
egen tag=tag(date)
sort date
gen multswitchrunsum_tmp=sum(tax_switch) if tag==1
egen multswitchrunsum=max(multswitchrunsum_tmp),by(date)
egen mult_mode=mode(multiplier),by(multswitchrunsum )
 
**CLEAN UP PRICES AND IDENTIFY SWITCHES IN PRICE
//im now cleaning up the price variable
qui gen price_cleaned = price
qui egen price_group = group(inventoryid price)
sort price_group
bysort price_group: gen price_group_count = _N
sort inventoryid date
*CW: added _n!=1 -- so never drop first price:
bysort inventoryid: replace price_cleaned = . if price_group_count==1&_n!=_N&_n!=1
sort inventoryid date
gen tmp=price_cleaned
gen price_oneoff_trans=0
replace price_oneoff_trans=1 if price_cleaned==.
bysort inventoryid: carryforward price_cleaned, replace 
*carry back instead when next price more appropriate than last price:
*the loop just repeats the same command six times because may have several one-off prices in a row:
forvalues i=1(1)6{
replace price_cleaned=price_cleaned[_n+1] if tmp==.&tmp[_n+1]<.&abs(price_cleaned[_n+1]-price[_n])<abs(price_cleaned[_n]-price[_n])&price_cleaned[_n+1]!=.
replace tmp=price_cleaned[_n+1] if tmp==.&tmp[_n+1]<.&abs(price_cleaned[_n+1]-price[_n])<abs(price_cleaned[_n]-price[_n])&price_cleaned[_n+1]!=.
}
drop tmp

//Identify changes in price 
sort inventoryid date
qui gen price_switched4 = 0 
bysort inventoryid: replace price_switched4 = 1 if price[_n]!=price[_n-1] &_n!=1	
	
**NOW LOOP THROUGH ALL THE MULTIPLIERS AND FIND THE ROUNDEST TRANSITION
sort inventoryid date	
qui egen rounder_group = group(multiplier)
qui sum rounder_group
local max = r(max)
	
	
//if there is only one multiplier candidate, we dont have to deal with the whole next bit	
local s = 1
if `max' == 1 {
di "There is only one multiplier"
	qui gen final_mult = multiplier
	
	//how round is each price?	
	qui gen price0 = price*final_mult
	qui gen round_price0 = round(price0, .25)
	qui gen check = abs(price0 - round_price0)
	*CW deleted this line because it takes .009 and rounds it to .01, quite bad for 99 cent issues. removed here and elsewhere: qui replace check = round(check,.001)
	qui replace check=round(check,.00001)
	qui gen round = 1 if check<=.01
	qui replace round = 0 if round==.
	*qui replace round=. if price!=price_cleaned
	qui rename check dist
	gen mult_1 = multiplier
	
	qui gen d_cut=.
	
	//calculate discount off last known round price, make discounts count as round 
	qui gen price1 = price*final_mult
	*CW: don't want this round: qui replace price1 = round(price1,.01)			
	qui gen last_round = price1 if round==1
	sort inventoryid date			
	bysort inventoryid: carryforward last_round, replace //we now have the last observed round price for each ob
	qui gen discount = price1/last_round
	*CW increase specificity re discount...may or may not be a good idea
	*replace discount = round(discount, .0001)
		
	forvalues c = .05(.05)1{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
		
	foreach c in .33 .67{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
				
	drop discount		

	gen d_cut_b4 = .
	//make intro discounts count as round 
	qui gen first_round = price1 if round==1
	gsort inventoryid -date			
	bysort inventoryid: carryforward first_round, replace			
	qui replace first_round = . if last_round!=.
	qui gen discount = price1/first_round
	*replace discount = round(discount, .0001)
	forvalues c = .05(.05)1{			
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99		
	}		
	foreach c in .33 .67{
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99
		}
	drop round_price0 discount last_round first_round price0 price1
					
	//replace round if it's a discount from a round price
	sort inventoryid date
	qui replace round = 1 if round==0&d_cut==1&round[_n-1]==1
	gsort inventoryid -date
	qui replace round = 1 if round==0&d_cut_b4==1&round[_n-1]==1
	qui replace dist = . if d_cut==1&round==1
	qui replace dist = . if d_cut_b4==1&round==1
	sort inventoryid date
	
	//calculate overall roundness and "distance to roundness" by inventorylot
	qui egen roundness = mean(round), by(inventoryid)
	qui egen distance = median(dist), by(inventoryid)
	*CW change: replace round(dist, with round(distance)
	qui replace distance=round(distance,.001)
	qui gen max_round = roundness //record the roundness
	qui gen min_dist = distance
	qui gen daily_round = round	
	qui gen round_1 = roundness
	drop roundness
	
	gen number_of_total_options = 1
	gen trans1flag  = 0 
	
	drop round dist d_cut* 
	 
mmerge date location using "cleaned_retail_data.dta", umatch(date salesloc) ukeep(inBusiness tothours1) unmatched(master)
	drop multiplier weekly
	
	save inv_formerging_`l'.dta, replace
	
	}
		 
//but if there are multiplier candidates, we need to look for transitions
else {	
sort inventoryid date
di "We are considering many multipliers"
forvalues r1 = 1(1)`max' {
	
	qui gen price0 = price_cleaned
	qui sum multiplier if rounder_group==`r1'
	local multiplier1 = r(mean)
	di `multiplier1'

forvalues r2 = 1(1)`max' {
	qui sum multiplier if rounder_group==`r2'
	local multiplier2 = r(mean)	
 
if `r1'==`r2' continue //skip the combination where they are both the same multiplier

	qui gen combination_`s' = 0		

		
	///////LOOK FOR TRANSITIONS///////////////	
	*rounder changes and the price0 changes by some "cent" amount (that cent amount can be 0)
	sort inventoryid date 
	
	//use the price switched 5 variable and look for transitions at any point (could also try price_switched4):
	sort inventoryid date
	bysort inventoryid: gen runsum=sum(price_switched4) 
	
	qui egen max_runsum=max(runsum), by(inventoryid)
	qui summ runsum
	local maxtrans = r(max)
	qui gen price1 = price*`multiplier1'
	qui gen price2 = price*`multiplier2'
	forvalues i=1(1)`maxtrans'{
	qui gen trans1_`i'=0
	qui replace trans1_`i'=1 if runsum>=`i'
	qui gen pricex=price1 if trans1_`i'==0
	qui replace pricex=price2 if trans1_`i'==1
	
	qui gen round_pricex = round(pricex, .25)
	qui gen check = abs(pricex - round_pricex)
	qui replace check=round(check,.00001)
	qui gen round = 1 if check<=.01
	qui replace round = 0 if round==.
	*qui replace round=. if price!=price_cleaned
	qui replace round=. if `i'>max_runsum+1
	qui egen roundtrans_`i'=mean(round) , by(inventoryid)
	drop pricex-round
	}
	
	qui gen max_trans_round=0
	qui gen min_count=0
	forvalues i=1(1)`maxtrans'{
	qui replace max_trans_round=roundtrans_`i' if roundtrans_`i'>max_trans_round&roundtrans_`i'<.
	}
	forvalues i=1(1)`maxtrans'{
	qui replace min_count=min_count+1 if roundtrans_`i'==max_trans_round
	}
	
	sort inventoryid date
	qui gen transplit=.
	forvalues i=1(1)`maxtrans'{
	qui replace transplit=trans1_`i' if roundtrans_`i'==max_trans_round&min_count==1
	}
	
	qui egen max_trans = max(transplit), by(inventoryid)
	qui egen min_trans = min(transplit), by(inventoryid)
	
	di `multiplier1'
	di `multiplier2'
	
	qui replace combination_`s' = .
	qui replace combination_`s' = `multiplier1' if transplit==0&max_trans==1&min_trans==0
	qui replace combination_`s' = `multiplier2' if transplit==1&max_trans==1&min_trans==0
 
	
	*CW note: this ignores any combos with multiple transitions of same roundedness and forces code to pick diff transition or non-transition. may want to change in the future.
				
	rename combination_`s' mult_transition_`s'
	 
	 
	drop  max_trans price1 price2 runsum-transplit min_trans max_trans
			
	local s = `s'+1
	}
	
	*rename price_switched5 price_switched5_`r1'
	drop price0 
	} //end considering all the multiplier combinations
 
 
////////////////////////////////////////////////////////////
//////calculate the roundness for all our options///////////
////////////////////////////////////////////////////////////
		
local option_tally = 1

	
**CALCULATING THE COMBOS
di "Calculating the combinations"
local pmax = `s'-1
forvalues p = 1(1)`pmax' {
	qui gen mult_`option_tally' = mult_transition_`p' //record the multipliers
		
	//how round is each price?	
	qui gen price0 = price*mult_`option_tally'
	qui gen round_price0 = round(price0, .25)
	qui gen check = abs(price0 - round_price0)
	*CW change: from <=.01 to <.02 and added the next line of code (and made the same change wherever this code repeats):
	*CW change: check needs to be rounded to .001 otherwise its all wrong when use to compute distance below (fixed here and elsewhere)
	*qui replace check = round(check,.001)
	qui replace check=round(check,.00001)
	qui gen round = 1 if check<=.01
	qui replace round = 0 if round==.
	*qui replace round=. if price!=price_cleaned
	qui rename check dist
	
	qui gen d_cut=.
	
	//calculate discount off last known round price, make discounts count as round 
	qui gen price1 = price*mult_`option_tally'
	*CW: shouldn't have this round, could do weird stuff with discounts as some rounds up and some rounds down. here and elsewhere: qui replace price1 = round(price1,.01)			
	qui gen last_round = price1 if round==1
	sort inventoryid date			
	bysort inventoryid: carryforward last_round, replace //we now have the last observed round price for each ob
	qui gen discount = price1/last_round
	*qui replace discount = round(discount, .01)
	
	*CW changed so this only goes to 1 (and elsewhere in code).  NOTE: previously some stuff went to .8, some stuff went to 1.  note: the way stata views this, it ends at c=.95
	forvalues c = .05(.05)1{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
		
	foreach c in .33 .67{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
				
	drop discount		

	gen d_cut_b4 = .
	//make intro discounts count as round 
	qui gen first_round = price1 if round==1
	gsort inventoryid -date			
	bysort inventoryid: carryforward first_round, replace			
	qui replace first_round = . if last_round!=.
	qui gen discount = price1/first_round
	*qui replace discount = round(discount, .01)
	forvalues c = .05(.05)1{			
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99			
	}		
	foreach c in .33 .67{
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99
		}
	drop discount last_round first_round price0	
					
	//replace round if it's a discount from a round price
	sort inventoryid date
	qui replace round = 1 if round==0&d_cut==1&round[_n-1]==1
	gsort inventoryid -date
	qui replace round = 1 if round==0&d_cut_b4==1&round[_n-1]==1
	qui replace dist = . if d_cut==1&round==1
	qui replace dist = . if d_cut_b4==1&round==1
	sort inventoryid date
	
	//calculate overall roundness and "distance to roundness" by inventorylot
	qui gen day_round_`option_tally' = round
		
	drop round_price0 round dist d_cut price1 d_cut_b4 
	local option_tally = `option_tally' + 1
	}
			
local max_combo_number = `option_tally' - 1	
qui gen number_of_combinations = `option_tally' - 1	


**CALCULATING THE NON-COMBOS
di "Calculating the non-combinations"
qui sum rounder_group
local max = r(max)
		
forvalues p = 1(1)`max'{
	di "this is the `option_tally'"
	qui sum multiplier if rounder_group==`p'
	local mult = r(mean)
			
	qui gen mult_`option_tally' = `mult'
	qui gen price0 = price*mult_`option_tally'
	qui gen round_price0 = round(price0, .25)
	qui gen check = abs(price0 - round_price0)
	*qui replace check = round(check,.001)
	qui replace check=round(check,.00001)
	qui gen round = 1 if check<=.01
	qui replace round = 0 if round==.
	*qui replace round=. if price!=price_cleaned
	qui rename check dist
	
		qui gen d_cut=.
	
	//calculate discount off last known round price, make discounts count as round 
	qui gen price1 = price*mult_`option_tally'
	qui gen last_round = price1 if round==1
	sort inventoryid date			
	bysort inventoryid: carryforward last_round, replace //we now have the last observed round price for each ob
	qui gen discount = price1/last_round
	
	forvalues c = .05(.05)1{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
		
	foreach c in .33 .67 {
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}		
				
	drop discount		

	qui gen d_cut_b4 = .
	//make intro discounts count as round 
	qui gen first_round = price1 if round==1
	gsort inventoryid -date			
	bysort inventoryid: carryforward first_round, replace			
	qui replace first_round = . if last_round!=.
	qui gen discount = price1/first_round
	*qui replace discount = round(discount, .01)
	forvalues c = .05(.05)1{			
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99		
	}		
		foreach c in .33 .67 {
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99
		}	
	drop discount last_round first_round price0	
					
	//replace round if it's a discount from a round price
	sort inventoryid date
	qui replace round = 1 if round==0&d_cut==1&round[_n-1]==1
	gsort inventoryid -date
	qui replace round = 1 if round==0&d_cut_b4==1&round[_n-1]==1
	qui replace dist = . if d_cut==1&round==1
	qui replace dist = . if d_cut_b4==1&round==1
	sort inventoryid date
	
	qui egen round_`option_tally' = mean(round), by(inventoryid) //record the roundness, same as "roundness" var
	qui gen day_round_`option_tally' = round
			
	drop round_price0 round dist d_cut price1 d_cut_b4
	local option_tally = `option_tally' + 1
	} 
	
local option_tally = `option_tally'  - 1	
qui gen number_of_total_options = `option_tally'	


***CALCULATING MULT_MODE ROUNDEDNESS:
di "Calculating mult-mode roundedness"
 
	qui gen price0 = price*mult_mode
	qui gen round_price0 = round(price0, .25)
	qui gen check = abs(price0 - round_price0)
	*qui replace check = round(check,.001)
	qui replace check=round(check,.00001)
	qui gen round = 1 if check<=.01
	qui replace round = 0 if round==.
	*qui replace round=. if price!=price_cleaned
	qui rename check dist
	
		qui gen d_cut=.
	
	//calculate discount off last known round price, make discounts count as round 
	qui gen price1 = price*mult_mode
	*CW: don't want this round: qui replace price1 = round(price1,.01)			
	qui gen last_round = price1 if round==1
	sort inventoryid date			
	bysort inventoryid: carryforward last_round, replace //we now have the last observed round price for each ob
	qui gen discount = price1/last_round
	*qui replace discount = round(discount, .01)
		
	forvalues c = .05(.05)1{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
		
	foreach c in .33 .67 {
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}		
				
	drop discount		

	qui gen d_cut_b4 = .
	//make intro discounts count as round 
	qui gen first_round = price1 if round==1
	gsort inventoryid -date			
	bysort inventoryid: carryforward first_round, replace			
	qui replace first_round = . if last_round!=.
	qui gen discount = price1/first_round
	*qui replace discount = round(discount, .01)
	forvalues c = .05(.05)1{			
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99		
	}		
		foreach c in .33 .67 {
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99
		}	
	drop discount last_round first_round price0	
					
	//replace round if it's a discount from a round price
	sort inventoryid date
	qui replace round = 1 if round==0&d_cut==1&round[_n-1]==1
	gsort inventoryid -date
	qui replace round = 1 if round==0&d_cut_b4==1&round[_n-1]==1
	qui replace dist = . if d_cut==1&round==1
	qui replace dist = . if d_cut_b4==1&round==1
	sort inventoryid date
	
	*CW comment: got rid of separate creation of vars and then renaming to speed up code:
	qui egen round_multmode = mean(round), by(inventoryid) //record the roundness, same as "roundness" var
	qui gen day_round_multmode = round
			
	drop round_price0 round dist d_cut price1 d_cut_b4
 
 
 

///////////////
//WHICH OPTION IS THE BEST
////////////////
di "Which option is the best"

qui gen max_round = round_multmode
qui gen final_mult = mult_mode
qui gen trans_flag = 0
qui gen trans_tie_flag = 0

*generate for later in this section: 
qui egen firstsaleinvdate=min(date),by(inventoryid)
qui egen lastsaleinvdate=max(date), by(inventoryid)

//we default to the mult_mode multiplier, but choose a transition multiplier if more round
	//figure out max roundness among these
	forvalues p = 1(1)`max_combo_number'{ 
		qui replace max_round = round_`p' if round_`p'>max_round&round_`p'!=.
	}

	//gen tie flag if multiple combos have the max roundness
	
	qui gen best_count = 0 
	forvalues p = 1(1)`max_combo_number'{ 
		qui replace best_count = best_count + 1 if round_`p'==max_round&max_round!=0
	}
	

	*if no ties, just pick the only transition multiplier
	forvalues p = 1(1)`max_combo_number' {
		qui replace final_mult = mult_`p' if round_`p'==max_round&best_count==1&max_round!=0&max_round>round_multmode //only replace if we actually have a transition combo
		qui replace trans_flag = 1 if round_`p'==max_round&best_count==1&max_round!=0&max_round>round_multmode
	}
	

	
	qui gen daily_round = day_round_multmode
	forvalues p = 1(1)`max_combo_number'{ 
	qui replace daily_round = day_round_`p' if round_`p'==max_round&best_count==1&max_round!=0&max_round>round_multmode
	}
	
	
	**if there was a one-off price at the transition, confirm that we got the correct multiplier
	forvalues p = 1(1)`max_combo_number'{
		qui sum mult_`p' if date==firstsaleinvdate
		local first = r(mean)
		qui sum mult_`p' if date==lastsaleinvdate
			local second = r(mean)
			qui replace final_mult = `first' if price_oneoff_trans==1&abs(price*`first' -round(price*`first', .25))<.02&abs(price*`second' -round(price*`second', .25))>.02&trans_flag==1&round_`p'==max_round&best_count==1&max_round!=0 
			}
		
//now we will replace final_mult if the non-transition mult is more round than any other multiplier already considered: 
	local start = `max_combo_number' + 1
		
	* what is the max roundness?
	qui gen tmpr=max_round
	forvalues p = `start'(1)`option_tally'{
	qui replace max_round = round_`p' if (round_`p'>max_round&round_`p'!=. )
}			

	*create a dummy for ties 
	qui replace best_count = 0 if trans_flag==0
	forvalues r = `start'(1)`option_tally'{
		qui replace best_count = best_count + 1 if round_`r'==max_round&max_round!=0&trans_flag==0
		}	

	*if no ties, pick the mult. if ties, switch back to what we had before:
	forvalues p = `start'(1)`option_tally'{
		qui replace final_mult = mult_`p' if round_`p'==max_round&max_round!=0&best_count<2 
		qui replace max_round=tmpr if  round_`p'==max_round&max_round!=0&best_count>=2 
		}	


			
	forvalues p = `start'(1)`option_tally'{
	qui replace daily_round = day_round_`p' if round_`p'==max_round&max_round!=0
	
	}		
	 drop tmpr					

	 
///////////////////////////////
///CLEAN UP////
//////////////////////////////

mmerge date location using "cleaned_retail_data.dta", umatch(date salesloc) ukeep(inBusiness tothours1) unmatched(master)
	
*mark which inventory lots never change price:
 		sort inventoryid
egen group=group(inventoryid)
sort group date
gen tmp=.
gen price0=round(price,.001)
replace tmp=1 if abs(price0[_n]-price0[_n-1])>.02&abs(price0[_n]-price0[_n-1])<.&group[_n]==group[_n-1]
replace tmp=0 if abs(2*price0[_n]-price0[_n-1])<.02&group[_n]==group[_n-1]
replace tmp=0 if abs(price0[_n]-2*price0[_n-1])<.02&group[_n]==group[_n-1]
egen totalpricetrans=total(tmp), by(group)
drop tmp group	price0

gen tie=final_mult==.
save invlevel_`name'.dta, replace
keep location inventoryid date price price_cleaned price_switched* final_mult max_round mult_* round_* usableweight daily_round inventorytype number_of_total_options trans*flag tie inBusiness tothours1 totalpricetrans  mult_flag
save inv_formerging_`l'.dta, replace

 
*now make plot:

drop if totalpricetrans==0&_N!=1
 drop if max_round<.90&_N!=1
 }
 /*
  if _N<=1 continue
format date %td

		qui sum location 
		local name = r(mean)
		*drop multiplier
		rename final_mult multiplier
		qui gen weekly = wofd(date+1)
		
	qui egen tot_weight=total(usableweight), by(weekly)
	qui egen inBusiness_week=min(inBusiness), by(weekly)
	qui replace inBusiness=inBusiness_week
	qui egen tothours_week=max(tothours1), by(weekly)
	qui replace tothours1=tothours_week
	drop inBusiness_week tothours_week
	
	/*sort date
	qui egen meanround=mean(daily_round) if multiplier<. , by(date)
	twoway  (line meanround date, yaxis(1)) , ///
	xline(20270, lcolor(black) lpattern(dash)) title("Daily Clean Roundedness, Firm `name'") ///
	ytitle("Roundedness", margin(medsmall))  xtitle("Date")  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	legend(region(lcolor(none))) xlabel(#6, valuelabel angle(forty_five)) 
	graph export roundedness_`name'.png, as(png) replace	
	*/

	

		sort inventoryid multiplier weekly date
		qui egen modemult=mode(multiplier), by(inventoryid)
		sort inventoryid multiplier weekly date
		bysort inventoryid: keep if _n==1	
		

		*replace multiplier=modemult
		tab multiplier, gen(mult)
		format multiplier %09.3f

		gsort -multiplier
		if multiplier==. continue
		qui egen group = group(multiplier)
		qui sum group 
		local max = r(max)
		


		forvalues r = 1(1)`max' {
		qui egen sum_mult`r'= sum(mult`r'), by(weekly)
 
		sum multiplier if group==`r'
		local name = round(r(mean), .001)
		label var  sum_mult`r' "mult = `name'"
		}
		
		/*local max2=`max'+1
		forvalues r = `max2'(1)`max2' {
		qui egen sum_mult`r'= sum(tie), by(weekly)

		local name ties
		label var  sum_mult`r' "mult = `name'"
		}*/
	

	
	*keep if date>20270-28&date<20270+28
	*keep if date<20037+28
	*keep if date<20270
	sum location 
	
	local name = r(mean)
	sort weekly
	*format weekly %tw
	capture noisily twoway (line sum_mult* weekly,yaxis(1)) ///
	(line inBusiness weekly, yaxis(2)) ///
	(line tothours weekly, yaxis(2)), ///
	xline(2886, lcolor(black) lpattern(dash)) title("First Transaction, Firm `name'") ///
	ytitle("Inventory Count", margin(medsmall))  xtitle("Week")  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	legend(region(lcolor(none))) xlabel(#6, valuelabel angle(forty_five)) 
	graph export first_`name'.png, as(png) replace	
 
*/
}

